//
//  VVContactsViewController.m
//  VVWenxinChat
//
//  Created by 王惠平 on 2017/5/10.
//  Copyright © 2017年 will wang. All rights reserved.
//

#import "VVContactsViewController.h"

@interface VVContactsViewController ()<NSFetchedResultsControllerDelegate>

//查询结果控制器
@property (nonatomic, strong) NSFetchedResultsController *fetchController;
//联系人列表
@property(nonatomic,strong)NSArray <XMPPUserCoreDataStorageObject*> *contacts;

@end

@implementation VVContactsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
  
    [self refreshData];
}

//刷新数据
- (void)refreshData {
    
    //执行查询
    BOOL success = [self.fetchController performFetch:nil];
    
    if (success) {
        
        //记录数据
        self.contacts = self.fetchController.fetchedObjects;
        
        //刷新界面
        [self.tableView reloadData];
    }
    
    
}

//上下文改变后调用
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    
    //刷新数据
    [self refreshData];
}

- (IBAction)clickAddFriendItem:(id)sender {
    
    
    [[VVXMPPManager sharedManager].xmppRoster addUser:[XMPPJID jidWithUser:@"yirenxin" domain:@"will.cn" resource:@"ios"] withNickname:@"一人心" ];

}


#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    
    return self.contacts.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"contact" forIndexPath:indexPath];
    
    //设置数据
    UILabel *jidLabel = [cell viewWithTag:1002];
    
    jidLabel.text = self.contacts[indexPath.row].jid.user;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
           // 删除好友
           [[VVXMPPManager sharedManager].xmppRoster removeUser:self.contacts[indexPath.row].jid];
    }
    
}


//懒加载
- (NSFetchedResultsController *)fetchController {
    
    if (_fetchController == nil) {
        
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPUserCoreDataStorageObject" inManagedObjectContext:[XMPPRosterCoreDataStorage sharedInstance].mainThreadManagedObjectContext];
        [fetchRequest setEntity:entity];
        // Specify criteria for filtering which objects to fetch
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"subscription == 'both'"];
        [fetchRequest setPredicate:predicate];
        // Specify how the fetched objects should be sorted
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"jidStr"ascending:YES];
        [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
        
        _fetchController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[XMPPRosterCoreDataStorage sharedInstance].mainThreadManagedObjectContext sectionNameKeyPath:nil cacheName:nil];
        
        _fetchController.delegate = self;
    }
    
    return _fetchController;
}


@end
